{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "432508f7",
   "metadata": {},
   "source": [
    "## 按字面创建列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d81580d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 这是个空列表\n",
    "prices = []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e33c107a",
   "metadata": {},
   "source": [
    "### 一个温度列表（华氏度），这是浮点数列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "80d6dc57",
   "metadata": {},
   "outputs": [],
   "source": [
    "temps = [32.0,212.0,0.0,81.6,100.0,45.3]\n",
    "# 对象（在这里就是浮点数）用逗号分隔，而且用中括号包围"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08e98c73",
   "metadata": {},
   "source": [
    "### 计算机编程中一个最著名的单词列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ed135de4",
   "metadata": {},
   "outputs": [],
   "source": [
    "words = ['hello','world']\n",
    "# 一个字符串列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d32a55a3",
   "metadata": {},
   "source": [
    "### 一个汽车详细信息的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4bda5295",
   "metadata": {},
   "outputs": [],
   "source": [
    "car_details = ['Toyota','RAV4',2.2,60807]\n",
    "# 包含不同类型对象的列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a5dabc5",
   "metadata": {},
   "source": [
    "## 让列表开始工作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4f6d2a6",
   "metadata": {},
   "source": [
    "### 使用“in”检查一个对象是否在另一个对象中吗"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f4867690",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "# # 检查一个单词：Milliways\n",
    "word = \"Milliways\"\n",
    "for letter in word:       \n",
    "    if letter in vowels:\n",
    "        print(letter)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81d1dcb3",
   "metadata": {},
   "source": [
    "## 在运行时“扩展”列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "85b5bba4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = []          # 建一个空列表\n",
    "len(found)             # 利用\"len\"确认其中多少对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7cbc7784",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found.append('a')      # 使用append方法在运行时将对象添加到列表末尾\n",
    "len(found)              # 长度也会增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "706d5e84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found                  # 确认对象是否在列表中"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7a4c199",
   "metadata": {},
   "source": [
    "### 使用not in检查一个集合中是否不存在某个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7a07daee",
   "metadata": {},
   "outputs": [],
   "source": [
    "if 'u' not in found:\n",
    "     found.append('u')             # 第一个的append调用会执行，因为'u'并不在其中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ab30f353",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'u']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "76f23cca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'u']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'u' not in found:\n",
    "     found.append('u')\n",
    "found                       # 下一个'append'不会执行，因为'u'已经在列表中 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce27acab",
   "metadata": {},
   "source": [
    "## 更新代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "f668f991",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = \"Milliways\"\n",
    "found = []            # 需要增加代码——创建空列表\n",
    "for letter in word:        # 取出单词中的各个字母\n",
    "    if letter in vowels:\n",
    "        if letter not in found:       # 增加 not in 来确定是否更新found元音列表\n",
    "            found.append(letter)\n",
    "for vowel in found:       # 第一个'for'循环结束时，第二个循环开始运行，它会显示'word'中找到的元音\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "75893726",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:marry\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")      # 增加交互性\n",
    "found = []            \n",
    "for letter in word:       \n",
    "    if letter in vowels:\n",
    "        if letter not in found:      \n",
    "            found.append(letter)\n",
    "for vowel in found:      \n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d438723d",
   "metadata": {},
   "source": [
    "## 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "5d3b40e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \"don't panic!\"\n",
    "plist = list(phrase)\n",
    "\n",
    "for i in range(4):     # 某个操作完成多次，使用for循环\n",
    "    plist.pop()         # 这个小循环从\"plist\"删除最后四个对象，不再有nic!\n",
    "plist.pop(0)            # 删除列表开头“D”\n",
    "plist.remove(\"'\")       # 在列表中找到引号这个值，并把他删除\n",
    "plist.extend([plist.pop(),plist.pop()])      # 交换列表末尾的两个对象位置，首先从对象删除这两个对象，然后使用删除的对象拓展列表\n",
    "plist.insert(2,plist.pop(3))           # 这行代码从列表删除空格，然后把它再插回到列表索引2的位置\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a325565",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e921d549",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
